package com.ruoyi.dynamic.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.util.concurrent.AbstractScheduledService;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.pojo.result.PageUtils;
import com.ruoyi.common.core.pojo.result.Pages;
import com.ruoyi.common.core.utils.CommonUtils;
import com.ruoyi.dynamic.mapper.RoomSingMapper;
import com.ruoyi.dynamic.model.constant.DynamicConstant;
import com.ruoyi.dynamic.model.constant.RoomChatRoomKvConstant;
import com.ruoyi.dynamic.model.pojo.entity.Room;
import com.ruoyi.dynamic.model.pojo.entity.RoomSing;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomSingAddRequest;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomSingDelRequest;
import com.ruoyi.dynamic.model.pojo.request.app.AppRoomSingSearchRequest;
import com.ruoyi.dynamic.model.pojo.result.app.AppRoomSetResult;
import com.ruoyi.dynamic.service.IMessageService;
import com.ruoyi.dynamic.service.IRoomService;
import com.ruoyi.dynamic.service.IRoomSetService;
import com.ruoyi.dynamic.service.IRoomSingService;
import com.ruoyi.system.api.RemoteAppUserService;
import com.ruoyi.system.api.model.user.AppUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.Optional;

/**
 * <p>
 * K歌房点歌记录 服务实现类
 * </p>
 *
 * @author Gnc
 * @since 2025-01-07
 */
@Service
public class RoomSingServiceImpl extends ServiceImpl<RoomSingMapper, RoomSing> implements IRoomSingService {

    @Autowired
    private IRoomService iRoomService;
    @Autowired
    private RemoteAppUserService remoteAppUserService;
    @Autowired
    private IRoomSetService iRoomSetService;
    @Autowired
    private IMessageService iMessageService;

    @Override
    public Pages<RoomSing> listPage(AppRoomSingSearchRequest request) {
        Page<RoomSing> roomSingPage = PageUtils.initPage(request, RoomSing.class);
        Page<RoomSing> page = lambdaQuery()
                .eq(!CommonUtils.isNull(request.getRoomId()), RoomSing::getRoomId, request.getRoomId())
                .orderByDesc(RoomSing::getIsTop)
                .orderByDesc(RoomSing::getCreateTime)
                .page(roomSingPage);
        return PageUtils.pages(page);
    }

    @Override
    public boolean add(AppRoomSingAddRequest request, Long userId) {
        Room verify = iRoomService.verify(request.getRoomId());
        Assert.isTrue(verify.getRoomTypeId().equals(DynamicConstant.RoomType.Music), "当前房间不是唱歌房无法点歌");

        AppRoomSetResult set = iRoomSetService.getUserRoomSet(verify.getUserId());
        Assert.isTrue(set.getIsSingMusic().equals(DynamicConstant.CheckStatus.YES), "未开启观众点歌主播唱");

        R<AppUser> userInfo = remoteAppUserService.getUserInfo(userId, SecurityConstants.APP);
        AppUser data = userInfo.getData();

        RoomSing roomSing = new RoomSing();
        BeanUtils.copyProperties(request, roomSing);
        roomSing.setRoomId(request.getRoomId());
        roomSing.setUserId(userId);
        roomSing.setUserNickName(data.getNickName());
        roomSing.setUserPhoto(data.getPhoto());
        roomSing.setCreateTime(new Date());
        return save(roomSing);
    }

    @Override
    public boolean removeSing(AppRoomSingDelRequest request, Long userId) {
        Room verify = iRoomService.verify(request.getRoomId());
        RoomSing sing = verify(request.getId());
        Assert.isTrue(sing.getIsPlay().equals(DynamicConstant.CheckStatus.NO), "当前歌正在播放中无法删除");

        Assert.isTrue(userId.equals(verify.getUserId()), "您无权操作");
        return lambdaUpdate().eq(RoomSing::getId, request.getId()).remove();
    }

    @Override
    public RoomSing verify(Long id) {
        Optional<RoomSing> roomSing = lambdaQuery()
                .eq(RoomSing::getId, id)
                .oneOpt();
        Assert.isTrue(roomSing.isPresent(), "点歌记录不存在");
        return roomSing.get();
    }

    @Override
    public boolean topSing(AppRoomSingDelRequest request, Long userId) {
        Room verify = iRoomService.verify(request.getRoomId());
        RoomSing sing = verify(request.getId());
        sing.setIsTop(DynamicConstant.CheckStatus.YES);
        return updateById(sing);
    }

    @Override
    public boolean playSing(AppRoomSingDelRequest request, Long userId) {
        Room verify = iRoomService.verify(request.getRoomId());
        RoomSing sing = verify(request.getId());
        sing.setIsPlay(DynamicConstant.CheckStatus.YES);
        updateById(sing);

        lambdaUpdate()
                .ne(RoomSing::getId, sing.getId())
                .set(RoomSing::getIsTop, DynamicConstant.CheckStatus.NO).update();

        iMessageService.setRoomChatRoomAttr(verify.getGroupId(), verify.getUserId().toString(),
                RoomChatRoomKvConstant.ROOM_SING_INFO, JSONObject.toJSONString(sing), 0, "", "");
        return true;
    }

    @Override
    public boolean stopSing(AppRoomSingDelRequest request, Long userId) {
        Room verify = iRoomService.verify(request.getRoomId());
        RoomSing sing = verify(request.getId());
        sing.setIsPlay(DynamicConstant.CheckStatus.NO);
        updateById(sing);

        iMessageService.setRoomChatRoomAttr(verify.getGroupId(), verify.getUserId().toString(),
                RoomChatRoomKvConstant.ROOM_SING_INFO, JSONObject.toJSONString(sing), 0, "", "");
        return true;
    }

    @Override
    public boolean isUserSing(Long roomId, Long userId) {
        return lambdaQuery().eq(RoomSing::getUserId, userId)
                .eq(RoomSing::getRoomId, roomId)
                .count() > 0
                ;
    }
}
